package com.example.myapplication

import android.content.res.Configuration
import android.os.Bundle


import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn


import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier

import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme

import kotlinx.coroutines.launch
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.compose.foundation.lazy.items


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Conversation(SampleData.conversationSample)
//                MessageCard(msg = Message("Android", "Jetpack Compose developer and first kotlin"))
////                HelloScreen()
////                MoviesScreen()
////                Greeting("Android")
            }
        }
    }
}


class HelloViewModel() {
    private val _name = MutableLiveData("")
    val name: LiveData<String> = _name
    fun onNameChange(newName: String) {
        _name.value = newName
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

data class Message(val author: String, val body: String)

@Composable
fun MessageCard(msg: Message) {
    Row(modifier = Modifier.padding(all = 8.dp)) {
        Image(
            painter = painterResource(id = R.drawable.profile_picture),
            contentDescription = null,
            modifier = Modifier
                .size(40.dp)
                .clip(CircleShape)
                .border(1.5.dp, MaterialTheme.colors.secondary, CircleShape)
        )
        Spacer(modifier = Modifier.width(8.dp))
        Column() {
            Text(
                msg.author,
                color = MaterialTheme.colors.secondaryVariant,
                style = MaterialTheme.typography.subtitle2
            )
            Spacer(modifier = Modifier.height(4.dp))
            Surface(shape = MaterialTheme.shapes.medium, elevation = 1.dp) {
                Text(
                    msg.body,
                    modifier = Modifier.padding(all = 4.dp),
                    style = MaterialTheme.typography.body2

                )
            }

        }
    }


}


@Composable
fun HelloScreen() {
//    var name:String by helloViewModel.name.observeAsState("")
    var name by rememberSaveable { mutableStateOf("") }
    HelloContent(name = name, onNameChang = { name = it })

}

@Composable
fun HelloContent(name: String, onNameChang: (String) -> Unit) {
    Column(modifier = Modifier.padding(16.dp)) {
        Text(
            text = "Hello,$name",
            modifier = Modifier.padding(bottom = 8.dp),
            style = MaterialTheme.typography.h5
        )

        OutlinedTextField(
            value = name, onValueChange = onNameChang,
            label = { Text(text = "Name") }
        )
    }
}


@Composable
fun MoviesScreen(scaffoldState: ScaffoldState = rememberScaffoldState()) {
    val scope = rememberCoroutineScope()
    Scaffold(scaffoldState = scaffoldState) {
        Column() {
            Button(onClick = { scope.launch { scaffoldState.snackbarHostState.showSnackbar("Something happened") } }) {
                Text(text = "Press me")
            }
        }
    }
}


@Composable
fun Conversation(messages: List<Message>) {

    LazyColumn {
        items(messages) { message ->
            MessageCard(msg = message)
        }
    }
}


@Preview(name = "Light Mode")
@Preview(
    uiMode = Configuration.UI_MODE_NIGHT_YES,
    showBackground = true,
    name = "Dark Mode"
)
@Composable
fun DefaultPreview() {
    Conversation(SampleData.conversationSample)
//    MessageCard(msg = Message("Colleague", "take me to your heart"))
//    HelloScreen()
//    MoviesScreen()
//    Greeting("Android")
}











